package com.androidRtc.dao;

import com.alibaba.fastjson.JSONObject;
import com.androidRtc.entity.Users;
import com.androidRtc.util.JdbcUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private JdbcUtil util = new JdbcUtil();
    //用户注册
    public int add(Users user){
        String sql = "insert into users(username,password,telephone,email) values(?,?,?,?)";
        PreparedStatement ps = util.createStatement(sql);
        int result = 0;
        try {
            ps.setString(1,user.getUserName());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getTelephone());
            ps.setString(4,user.getEmail());
            result = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally{
            util.close();
        }
        return result;

    }

    public JSONObject findData(String username){
        String sql = "select telephone,email from users where username = ?";
        PreparedStatement ps = util.createStatement(sql);
        ResultSet rs = null;
        JSONObject json = new JSONObject();
        try {
            ps.setString(1,username);
            rs = ps.executeQuery();
            while(rs.next()){
                String telephone = rs.getString("telephone");
                String email = rs.getString("email");
                json.put("telephone",telephone);
                json.put("email",email);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            util.close(rs);
        }
        return json;
    }
    //用户登录
    public int login(String username,String password){
        String sql = "select count(*) from users where username = ? and password = ?";
        PreparedStatement ps = util.createStatement(sql);
        ResultSet rs = null;
        int result = 0;
        try {
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while(rs.next()){
                result = rs.getInt("count(*)");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            util.close(rs);
        }
        return result;
    }

    //修改密码
    public int update(String username,String password){
        String sql = "update users set password = ? where username = ?";
        PreparedStatement ps = util.createStatement(sql);
        int result = 0;
        try{
            ps.setString(1,password);
            ps.setString(2,username);
            result = ps.executeUpdate();
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }finally {
            util.close();
        }
        return result;
    }
    //修改用户信息
    public int update(String username,String telephone,String email){
        String sql = "update users set telephone = ? , email = ? where username = ?";
        PreparedStatement ps = util.createStatement(sql);
        int result = 0;
        try {
            ps.setString(1,telephone);
            ps.setString(2,email);
            ps.setString(3,username);
            result = ps.executeUpdate();
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }finally {
            util.close();
        }
        return result;
    }

}
